package cn.dnui_wnx725.dnuifood_16110100725.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.CollectShopAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.bean.CollectListBean;
import cn.dnui_wnx725.dnuifood_16110100725.controller.FoodActivity;
import cn.dnui_wnx725.dnuifood_16110100725.listener.ListListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.CollectListModel;

public class FavorShopFragment extends BaseFragment{
    private RecyclerView recyclerView;
    private String flag="0";
    private RecyclerView.LayoutManager layoutManager;
    private SwipeRefreshLayout mSwipeLayout;

    ListListener<CollectListBean> listBeanListListener = new ListListener<CollectListBean>() {
        @Override
        public void onResponse(final List<CollectListBean> list) {
            final CollectShopAdapter collectShopAdapter = new CollectShopAdapter(getActivity(),list, R.layout.item);
            CollectShopAdapter.OnItemClickListener onItemClickListener = new CollectShopAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(getActivity(), FoodActivity.class);
                    intent.putExtra("shopid",list.get(position).getShop_id());
                    startActivity(intent);
                }
            };
//            collectShopAdapter.setList(list);
            collectShopAdapter.setmOnItemClickListener(onItemClickListener);
            recyclerView.setAdapter(collectShopAdapter);
            mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    collectShopAdapter.setList(list);
                    collectShopAdapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.fragment_favor_shop);
        recyclerView=view.findViewById(R.id.rv_favor_shop);
        mSwipeLayout = view.findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        layoutManager =new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
    }

    @Override
    void initEvent() {
        Toast.makeText(getActivity(),getUserID(),Toast.LENGTH_SHORT).show();
        CollectListModel collectListModel = new CollectListModel();
        collectListModel.getCollectList(getUserID(),flag,listBeanListListener);
    }

    @Override
    void initData() {

    }
    private void request_list(){
        CollectListModel collectListModel = new CollectListModel();
        collectListModel.getCollectList(getUserID(),"0",listBeanListListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getActivity(),getUserID(),Toast.LENGTH_SHORT).show();
        request_list();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_favor_shop);
        return super.onCreateView(inflater,container,savedInstanceState);
    }

}